home *** CD-ROM | disk | FTP | other *** search
/ Champak 66 / Vol 66.iso / games / bob_espo.swf / scripts / __Packages / Sounds.as < prev    next >
Encoding:
Text File  |  2013-04-24  |  7.7 KB  |  290 lines

  1. class Sounds
  2. {
  3.    var mcTimeLine;
  4.    var mcRef;
  5.    var bMuteSound;
  6.    var bMuteMusic;
  7.    var oSounds;
  8.    var nDepth;
  9.    var aTabSonFade;
  10.    var nFadingSpeed;
  11.    static var nFADE_SPEED = 5;
  12.    function Sounds(_mcTimeline)
  13.    {
  14.       this.mcTimeLine = _mcTimeline;
  15.       this.init();
  16.       this.mcRef = _mcTimeline.mcSounds;
  17.       this.mcRef.onEnterFrame = Delegate.create(this,this.enterFrame);
  18.    }
  19.    function isSoundsMuted()
  20.    {
  21.       return this.bMuteSound;
  22.    }
  23.    function isMusicMuted()
  24.    {
  25.       return this.bMuteMusic;
  26.    }
  27.    function playSound(_sName, _nVolume, _nLoop)
  28.    {
  29.       if(!this.exist(_sName))
  30.       {
  31.          this.oSounds[_sName] = new Object();
  32.          this.oSounds[_sName].sName = _sName;
  33.          this.oSounds[_sName].nVolume = 0;
  34.          this.oSounds[_sName].nPausePosition = 0;
  35.          this.oSounds[_sName].mcRef = this.mcTimeLine.createEmptyMovieClip(_sName,this.nDepth);
  36.          this.oSounds[_sName].sSound = new Sound(_sName);
  37.          this.oSounds[_sName].sSound.attachSound(_sName);
  38.          if(_sName == Controller.PACKAGING_INTRO_MUSIC_NAME)
  39.          {
  40.             this.oSounds[_sName].sSound.onSoundComplete = Delegate.create(Controller.getRef(),Controller.getRef().introComplete);
  41.          }
  42.          this.nDepth = this.nDepth + 1;
  43.       }
  44.       if(this.exist(_sName))
  45.       {
  46.          this.setVolumeSound(_sName,_nVolume);
  47.          this.oSounds[_sName].sSound.start(0,_nLoop);
  48.          if(this.isMusic(_sName))
  49.          {
  50.             if(this.bMuteMusic)
  51.             {
  52.                this.oSounds[_sName].sSound.setVolume(0);
  53.             }
  54.          }
  55.          else if(this.bMuteSound)
  56.          {
  57.             this.oSounds[_sName].sSound.setVolume(0);
  58.          }
  59.       }
  60.    }
  61.    function playRandomSound(_nMaxSounds, _sSoundName, _nVolume)
  62.    {
  63.       var _loc5_ = Math.round(Math.random() * (_nMaxSounds - 1));
  64.       _loc5_ = _loc5_ + 1;
  65.       this.playSound(_sSoundName + "(" + _loc5_ + ")",_nVolume,1);
  66.    }
  67.    function stopSound(_sName)
  68.    {
  69.       if(this.exist(_sName))
  70.       {
  71.          this.setVolumeSound(_sName,0);
  72.          this.oSounds[_sName].sSound.stop();
  73.       }
  74.    }
  75.    function pauseSound(_sName)
  76.    {
  77.       if(this.exist(_sName))
  78.       {
  79.          if(!this.isMusic(_sName))
  80.          {
  81.             if(this.oSounds[_sName].nPausePosition == -1)
  82.             {
  83.                this.oSounds[_sName].nPausePosition = 0;
  84.             }
  85.             else
  86.             {
  87.                this.oSounds[_sName].nPausePosition = this.oSounds[_sName].sSound.position;
  88.             }
  89.             this.oSounds[_sName].sSound.stop();
  90.          }
  91.       }
  92.    }
  93.    function resumeSound(_sName)
  94.    {
  95.       if(this.exist(_sName))
  96.       {
  97.          if(!this.isMusic(_sName))
  98.          {
  99.             if(this.oSounds[_sName].nPausePosition != 0 && this.oSounds[_sName].nPausePosition != undefined)
  100.             {
  101.                this.oSounds[_sName].sSound.start(this.oSounds[_sName].nPausePosition / 1000,0);
  102.             }
  103.          }
  104.       }
  105.    }
  106.    function pauseAllSounds()
  107.    {
  108.       for(var i in this.oSounds)
  109.       {
  110.          this.pauseSound(this.oSounds[i].sName);
  111.       }
  112.    }
  113.    function resumeAllSounds()
  114.    {
  115.       for(var i in this.oSounds)
  116.       {
  117.          this.resumeSound(this.oSounds[i].sName);
  118.       }
  119.    }
  120.    function startFadeIn(_sName, _nVolume, _nLoop)
  121.    {
  122.       if(!this.bMuteSound && !this.isMusic(_sName) || !this.bMuteMusic && this.isMusic(_sName))
  123.       {
  124.          this.playSound(_sName,0,_nLoop);
  125.          var _loc5_ = [_sName,_nVolume,"In",0];
  126.          this.aTabSonFade.push(_loc5_);
  127.       }
  128.       else
  129.       {
  130.          this.playSound(_sName,_nVolume,_nLoop);
  131.       }
  132.    }
  133.    function startFadeOut(_sName)
  134.    {
  135.       if(this.exist(_sName))
  136.       {
  137.          this.aTabSonFade = [];
  138.          if(this.oSounds[_sName].sSound.getVolume() != 0)
  139.          {
  140.             var _loc3_ = [_sName,0,"Out",this.oSounds[_sName].sSound.getVolume()];
  141.             this.aTabSonFade.push(_loc3_);
  142.          }
  143.          else
  144.          {
  145.             this.stopSound(_sName);
  146.          }
  147.       }
  148.    }
  149.    function setVolumeSound(_sName, _nVolume)
  150.    {
  151.       if(this.exist(_sName))
  152.       {
  153.          this.oSounds[_sName].nVolume = _nVolume;
  154.          if(this.bMuteSound && !this.isMusic(_sName) || this.bMuteMusic && this.isMusic(_sName))
  155.          {
  156.             this.oSounds[_sName].sSound.setVolume(0);
  157.          }
  158.          else
  159.          {
  160.             this.oSounds[_sName].sSound.setVolume(_nVolume);
  161.          }
  162.       }
  163.    }
  164.    function DoMuteSounds()
  165.    {
  166.       this.bMuteSound = true;
  167.       for(var i in this.oSounds)
  168.       {
  169.          if(!this.isMusic(this.oSounds[i].sName))
  170.          {
  171.             this.oSounds[this.oSounds[i].sName].sSound.setVolume(0);
  172.          }
  173.       }
  174.    }
  175.    function UndoMuteSounds()
  176.    {
  177.       this.bMuteSound = false;
  178.       for(var i in this.oSounds)
  179.       {
  180.          if(!this.isMusic(this.oSounds[i].sName))
  181.          {
  182.             this.setVolumeSound(this.oSounds[i].sName,this.oSounds[i].nVolume);
  183.          }
  184.       }
  185.    }
  186.    function DoMuteMusic()
  187.    {
  188.       this.bMuteMusic = true;
  189.       for(var i in this.oSounds)
  190.       {
  191.          if(this.isMusic(this.oSounds[i].sName))
  192.          {
  193.             this.oSounds[this.oSounds[i].sName].sSound.setVolume(0);
  194.          }
  195.       }
  196.    }
  197.    function UndoMuteMusic()
  198.    {
  199.       this.bMuteMusic = false;
  200.       for(var i in this.oSounds)
  201.       {
  202.          if(this.isMusic(this.oSounds[i].sName))
  203.          {
  204.             this.setVolumeSound(this.oSounds[i].sName,this.oSounds[i].nVolume);
  205.          }
  206.       }
  207.    }
  208.    function enterFrame()
  209.    {
  210.       for(var i in this.aTabSonFade)
  211.       {
  212.          var _loc2_ = this.aTabSonFade[i][0];
  213.          var _loc3_ = this.aTabSonFade[i][1];
  214.          var _loc4_ = this.aTabSonFade[i][2];
  215.          var _loc5_ = this.aTabSonFade[i][3];
  216.          if(_loc4_ == "In")
  217.          {
  218.             _loc5_ += this.nFadingSpeed;
  219.             if(_loc5_ >= _loc3_)
  220.             {
  221.                _loc5_ = _loc3_;
  222.                this.deleteFading(_loc2_);
  223.                if(this.aTabSonFade.length == 0)
  224.                {
  225.                   delete this.oSounds[_loc2_].mcRef.onEnterFrame;
  226.                }
  227.             }
  228.          }
  229.          else
  230.          {
  231.             _loc5_ -= this.nFadingSpeed;
  232.             if(_loc5_ <= 0)
  233.             {
  234.                this.stopSound(_loc2_);
  235.                _loc5_ = 0;
  236.                this.deleteFading(_loc2_);
  237.                if(this.aTabSonFade.length == 0)
  238.                {
  239.                   delete this.oSounds[_loc2_].mcRef.onEnterFrame;
  240.                }
  241.             }
  242.          }
  243.          if(_loc5_ != 0)
  244.          {
  245.             this.aTabSonFade[i][3] = _loc5_;
  246.          }
  247.          this.setVolumeSound(_loc2_,_loc5_);
  248.       }
  249.    }
  250.    function deleteFading(_sName)
  251.    {
  252.       var _loc3_ = 0;
  253.       while(_loc3_ <= this.aTabSonFade.length)
  254.       {
  255.          if(this.aTabSonFade[_loc3_][0] == _sName)
  256.          {
  257.             this.aTabSonFade.splice(_loc3_,1);
  258.          }
  259.          _loc3_ = _loc3_ + 1;
  260.       }
  261.    }
  262.    function init()
  263.    {
  264.       this.oSounds = {};
  265.       this.aTabSonFade = [];
  266.       this.bMuteMusic = false;
  267.       this.bMuteSound = false;
  268.       this.nDepth = 0;
  269.       this.nFadingSpeed = Sounds.nFADE_SPEED;
  270.    }
  271.    function exist(_sName)
  272.    {
  273.       var _loc3_ = false;
  274.       if(this.oSounds[_sName])
  275.       {
  276.          _loc3_ = true;
  277.       }
  278.       return _loc3_;
  279.    }
  280.    function isMusic(_sName)
  281.    {
  282.       var _loc3_ = false;
  283.       if(_sName == Controller.GAME_MUSIC_NAME || (_sName == Controller.PACKAGING_INTRO_MUSIC_NAME || _sName == Controller.PACKAGING_MUSIC_NAME))
  284.       {
  285.          _loc3_ = true;
  286.       }
  287.       return _loc3_;
  288.    }
  289. }
  290.